package com.czl.module_storehouse.activity.warn;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.czl.base.config.AppConstants;
import com.czl.base.data.bean.tengyun.StorehouseBean;
import com.czl.module_base.activity.BaseRecyclerViewActivity;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.mvp.view.SimpleView;
import com.czl.module_service.event.StockWarnBeanEvent;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.adapter.StockWarnAdapter;
import com.czl.module_storehouse.databinding.LayoutRecyclerViewBinding;
import com.czl.module_storehouse.mvp.presenter.StockPresenter;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
@Route(path = AppConstants.Router.Service.F_SERVICE_STOCK_WARN)
public class StockWarnActivity extends BaseRecyclerViewActivity<LayoutRecyclerViewBinding> implements SimpleView {

    @InjectPresenter
    StockPresenter mStockPresenter;
    private StockWarnAdapter mAdapter;

    @Override
    protected LayoutRecyclerViewBinding getChildBinding(LayoutInflater layoutInflater, FrameLayout containerTop) {
        return null;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("库存预警");
        mAdapter = new StockWarnAdapter(R.layout.item_stock_warn, new ArrayList<>());
        binding.recyclerView.setAdapter(mAdapter);
        if (getIntent() != null) {
            String storehouseId = getIntent().getStringExtra("storehouseId");
            if (!TextUtils.isEmpty(storehouseId)) {
                mStockPresenter.getStorehouseStockWarnList(storehouseId);
            }
        }
        LiveEventBus.get(StockWarnBeanEvent.class).observeSticky(this, event -> initAdapter(event.getStockWarnBeanList()));
    }

    private void initAdapter(List<StorehouseBean.StockWarnBean> list) {
        mAdapter.addData(list);
        if (list.isEmpty()) {
            View emptyView = View.inflate(getContext(), com.czl.module_base.R.layout.common_empty_layout, null);
            mAdapter.setEmptyView(emptyView);
        }
        binding.smartRefreshLayout.setEnableRefresh(false);
        binding.smartRefreshLayout.setEnableLoadMore(false);
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        handleResponseData(mAdapter, mStockPresenter.getPageNo(), (ListBean<StorehouseBean.StockWarnBean>) t);
    }
}
